$(function () {

    function renderForm(){
        layui.use('form', function(){
            var form = layui.form;
            form.render();
        });
    }
    $.ajax({
        type: 'POST',
        url: '/enable/getMerchantMsg',
        async: false,
        data: {},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                var merchantCount = result.data.merchantList.length;
                var count = 0;
                for (i = 0;i<merchantCount;i++) {
                    if (result.data.merchantList[i].state==0){
                        $('#likeCheckBox').append('<input type="checkbox" name="accountName" data="'+result.data.merchantList[i].id+'" lay-skin="primary" title=""><a class="merchant-Msg " onclick="selectMerchant('+result.data.merchantList[i].id+');">'+result.data.merchantList[i].name+'</a><br>');
                        if (count==0) {
                            selectMerchant(result.data.merchantList[i].id);count++;
                        }
                    renderForm();
                    }else  if (result.data.merchantList[i].state==1) {
                        $("#able-merchant").append("<div class='able-a-merchant'><a class='able-a-merchant ' onclick='selectMerchant("+result.data.merchantList[i].id+");'>"+result.data.merchantList[i].name+"</a></div>");
                    }
                }
                $('#likeCheckBox').append("<button class=\"btn green\" type=\"button\" lay-filter=\"formDemo\" style=\"position: relative;top: 10px;left: 140px;\" id=\"all\" onclick='updateState();'>勾选批量启用</button>");
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });

});
$("#disable_user").click(function () {
    if ( $(".xia").css("display")=="none"){
        $('.disable-outer-container').slideUp(100,function(){
            $(".xia").css("display","block");
            $(".shang").css("display","none");
        });
    }else{

        $('.disable-outer-container').slideDown(100,function() {
            $(".xia").css("display", "none");
            $(".shang").css("display", "block");
        });
    }

});
function selectMerchant(id) {
    $("#account_table").empty();
    $.ajax({
        type: 'POST',
        url: '/enable/merchantDetail',
        async: false,
        data: {"merchantId":id},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                $("input[name='name']").val(result.data.merchant.name);
                $("input[name='id']").val(result.data.merchant.id);
                $("input[name='code']").val(result.data.merchant.code);
                $("input[name='wallet']").val("预付费子账户钱包");
                $("input[name='p1']").val(result.data.merchant.p1);
                $("input[name='phone']").val(result.data.merchant.phone1);
                $("input[name='phone1']").val(result.data.merchant.phone1);
                $("input[name='mail1']").val(result.data.merchant.mail1);
                $("input[name='addr1']").val(result.data.merchant.addr1);
                $("input[name='p2']").val(result.data.merchant.p2);
                $("input[name='phone2']").val(result.data.merchant.phone2);
                $("input[name='mail2']").val(result.data.merchant.mail2);
                $("input[name='addr2']").val(result.data.merchant.addr2);
                var accountlength =result.data.accountList.length;
                if (accountlength>0){
                    for (var i=0;i<accountlength;i++){
                        var power_type = gettype(result.data.accountList[i].powertype);
                        var charge = gettype(result.data.accountList[i].charge);
                        var accountStr ="<div class=\"col-md-3 col-msg\"style=\"max-width: 20%\">\n" +
                            "                                    <input type=\"text\" placeholder=\"\" maxlength=\"2000\" style=\"width: 112%;\"\n" +
                            "                                            value=\""+result.data.accountList[i].name+"\" class=\"form-control msg-val\" />\n" +
                            "                                </div>\n" +
                            "                                <div class=\"col-md-3 col-msg\" style=\"max-width: 20%\">\n" +
                            "                                    <input type=\"text\" placeholder=\"\" maxlength=\"2000\" style=\"width: 112%;\"\n" +
                            "                                            value=\""+power_type+"\" class=\"form-control msg-val\" />\n" +
                            "                                </div>\n" +
                            "                                <div class=\"col-md-3 col-msg\" style=\"max-width: 20%\">\n" +
                            "                                    <input type=\"text\" placeholder=\"\" maxlength=\"2000\" style=\"width: 112%;\"\n" +
                            "                                            value=\""+result.data.accountList[i].collectors+"\" class=\"form-control msg-val\" />\n" +
                            "                                </div>\n" +
                            "                                <div class=\"col-md-3 col-msg\"  style=\"max-width: 20%\">\n" +
                            "                                    <input type=\"text\" placeholder=\"\" maxlength=\"2000\" style=\"width: 112%;\"\n" +
                            "                                           value=\""+result.data.accountList[i].readingString+"\" class=\"form-control msg-val\" />\n" +
                            "                                </div>\n" +
                            "                                <div class=\"col-md-3 col-msg\" style=\"max-width: 20%\" >\n" +
                            "                                    <input type=\"text\" placeholder=\"\" maxlength=\"2000\" style=\"width: 112%;\"\n" +
                            "                                           value=\""+charge+"费\" class=\"form-control msg-val\" />\n" +
                            "                                </div>"

                $("#account_table").append(accountStr);
                    }
                }
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });
}

function  gettype(str) {
    if (str==0){
        return "电";
    }else if (str==1) {
        return "水";
    }else{
        return "气";
    }
}
function modifyMerchant() {
    var name = $("input[name='name']").val();
    var id = $("input[name='id']").val();
    var p1 = $("input[name='p1']").val();
    var phone1 =$("input[name='phone1']").val();
    var mail1 =$("input[name='mail1']").val();
    var addr1 =$("input[name='addr1']").val();
    var p2 = $("input[name='p2']").val();
    var phone2 = $("input[name='phone2']").val();
    var mail2 = $("input[name='mail2']").val();
    var addr2 = $("input[name='addr2']").val();
    var  re = /^1\d{10}$/;
    if (!re.test(phone1)) {
        layer.msg('手机号格式有误！', {icon: 5});
    }else if (phone2!=null&&phone2!=""&&!re.test(phone2)) {
        layer.msg('手机号格式有误！', {icon: 5});
    }else{
    $.ajax({
        type: 'POST',
        url: '/enable/modifyMerchant',
        async: false,
        data: {"name":name,"id":id,"p1":p1,"phone1":phone1,"mail1":mail1,"addr1":addr1,"p2":p2,"phone2":phone2,"mail2":mail2,"addr2":addr2,},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('保存成功',{icon:1}, function(index){
                        //do something
                        layer.close(index);
                        selectMerchant(id);
                    });
                });
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });
    }
}
function updateState() {
    //获取checkbox的值
    var arr = new Array();
    $("input:checkbox[name='accountName']:checked").each(function(i) { // 遍历name=test的多选框
        arr[i] =  $(this).attr("data");
    });
    var merchants = arr.join(",")
    $.ajax({
        type: 'POST',
        url: '/enable/updateMerchantState',
        async: false,
        data: {"merchants":merchants},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('启用成功',{icon:1}, function(index){
                        //do something
                        layer.close(index);
                        location.reload(false);
                    });

                });
            }else{

            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });

}
$("#enable").click(function () {
    var id = $("input[name='id']").val();
    $.ajax({
        type: 'POST',
        url: '/enable/updateMerchantState',
        async: false,
        data: {"merchants":id},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('启用成功',{icon:1}, function(index){
                        //do something
                        layer.close(index);
                        location.reload(false);
                    });
                });
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });
});